const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const plugins = [
];

if (process.env.NODE_ENV === 'production') {
  plugins.push(new webpack.IgnorePlugin(/redux-logger/));
}

module.exports = (server = false) => {
  return {
    resolve: {
      modules: ['node_modules'],
      alias: {
        'ywen-mobile-ui/assets/styles/index.less': path.resolve(__dirname, '../node_modules/ywen-mobile-ui/assets/styles/index.less'),
        'ywen-mobile-ui': path.resolve(__dirname, '../node_modules/ywen-mobile-ui/dist/index.js'),
      },
      extensions: ['.jsx', '.js', '.less'],
    },
    plugins,
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [
            {
              loader: MiniCssExtractPlugin.loader,
              options: {
                // you can specify a publicPath here
                // by default it use publicPath in webpackOptions.output
              },
            },
            // 'style-loader',
            'css-loader', {
              loader: 'postcss-loader',
              options: {
                config: {
                  path: path.resolve(__dirname, '..', 'postcss.config.js'),
                },
              },
            },
          ],
        },
        {
          test: /\.less$/,
          use: [
            {
              loader: MiniCssExtractPlugin.loader,
              options: {
                // you can specify a publicPath here
                // by default it use publicPath in webpackOptions.output
              },
            },
            // 'style-loader',
            'css-loader',
            {
              loader: 'postcss-loader',
              options: {
                config: {
                  path: path.resolve(__dirname, '..', 'postcss.config.js'),
                },
              },
            },
            'less-loader',
          ],
        },
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/,
          use: [
            {
              loader: 'file-loader',
              options: {
                emitFile: !server,
              },
            },
          ],
        },
        {
          test: /\.(png|svg|jpe?g|gif)$/,
          use: [
            {
              loader: 'file-loader',
              options: {
                emitFile: !server,
              },
            },
          ],
        },
        {
          test: /\.jsx?$/,
          include: [
            path.resolve(__dirname, '../common/'),
            path.resolve(__dirname, '../server/'),
            path.resolve(__dirname, '../client/'),
            path.resolve(__dirname, '../node_modules/ywen-mobile-ui/'),
          ],
          exclude: /node_modules/,
          use: [
            {
              loader: 'babel-loader',
            },
          ],
        },
      ],
    },
  };
};
